 //! Commands 插件向外暴露的公共接口

use std::any::Any;
use std::sync::Arc;

// 定义清理函数的类型
pub type Disposer<'a> = Box<dyn FnOnce() + Send + Sync + 'a>;

/// 命令处理器的 Trait
/// 所有具体的命令逻辑都需要实现这个 Trait
pub trait ICommandHandler: Send + Sync {
    fn execute(&self, args: Arc<dyn Any + Send + Sync>) -> Result<Arc<dyn Any + Send + Sync>, String>;
}

/// 命令服务的上下文 Trait
/// 其他插件通过这个 Trait 来与命令系统交互
pub trait CommandsContext<'a> {
    /// 注册一个命令
    fn register(&self, command_name: &str, handler: Box<dyn ICommandHandler>) -> Disposer<'a>;

    /// 执行一个命令
    fn execute(&self, command_name: &str, args: Arc<dyn Any + Send + Sync>) -> Result<Arc<dyn Any + Send + Sync>, String>;
}
